package com.ruoyi.project.kpi.service.impl;

import com.ruoyi.project.device.ddjqueue.domain.WmsDeviceExecutionQueue;
import com.ruoyi.project.device.ddjqueue.mapper.WmsDeviceExecutionQueueFinishMapper;
import com.ruoyi.project.device.space.domain.BasCargoSpace;
import com.ruoyi.project.device.space.mapper.BasCargoSpaceMapper;
import com.ruoyi.project.kpi.service.KpiService;
import com.ruoyi.project.mydo.mydojob.domain.MydoJob;
import com.ruoyi.project.mydo.mydojob.domain.MydoJobFinish;
import com.ruoyi.project.mydo.mydojob.mapper.MydoJobFinishMapper;
import com.ruoyi.project.stock.stock.domain.BasStock;
import com.ruoyi.project.stock.stock.mapper.BasStockMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class KpiServiceImpl implements KpiService {

   @Autowired
   private BasCargoSpaceMapper basCargoSpaceMapper;

   @Autowired
   private BasStockMapper basStockMapper;

   @Autowired
   private MydoJobFinishMapper mydoJobFinishMapper;

   @Autowired
   private WmsDeviceExecutionQueueFinishMapper wdeqfm;

   @Override
//   public Map<String, Long> getCargoSpaceAnalysis(BasCargoSpace space) {
//      Map<String, Long> map = new HashMap<>();
//      for (int i = 1; i < 7; i++) {
//         space.setStatus(i+"");
//         Integer status = basCargoSpaceMapper.findByStatus(space);
//         map.put("s" + i, Long.valueOf(status));
//      }
//      return map;
//   }
   public Map<String, Long> getCargoSpaceAnalysis(BasCargoSpace space) {
      Map<String, Long> map = new HashMap<>();

      for (int i = 1; i < 7; i++) {
         space.setStatus(i+"");
         Integer status = basCargoSpaceMapper.findByStatus(space);
         map.put("s" + i, Long.valueOf(status));
      }



      return map;
   }


   @Override
   public Map<String, Object> getCargoSpacePlan(BasCargoSpace space) {
      String empty ="";
      String full ="";
      String inAllot ="";
      String outAllot ="";
      String inCheck ="";
      String outCheck ="";
      String floor ="";
      String column ="";

      /**
       * 获取列数据
       */
      List<BasCargoSpace> columns = basCargoSpaceMapper.getcolumn(space);
      for(BasCargoSpace col : columns){
         column =column + "'"+col.getTheColumn()+"列',";
      }
      column ="["+column+"]";
      /**
       * 获取层数据
       */
      List<BasCargoSpace> floors = basCargoSpaceMapper.getfloor(space);
      for(BasCargoSpace flo : floors){
         floor =floor + "'"+flo.getTheFloor()+"层',";
      }
      floor ="["+floor+"]";

      List<BasCargoSpace> spaces = basCargoSpaceMapper.allstatus(space);
      for (BasCargoSpace space1 : spaces) {
         int status = Integer.parseInt(space1.getStatus());
         if(status ==1){
            empty = empty +"[" +space1.getTheColumn()+","+ space1.getTheFloor()+","+status+"],";
         }
         if(status ==2){
            full = full +"[" +space1.getTheColumn()+","+ space1.getTheFloor()+","+status+"],";
         }
         if(status ==3){
            inAllot = inAllot +"[" +space1.getTheColumn()+","+ space1.getTheFloor()+","+status+"],";
         }
         if(status ==4){
            outAllot = outAllot +"[" +space1.getTheColumn()+","+ space1.getTheFloor()+","+status+"],";
         }
         if(status ==5){
            inCheck = inCheck +"[" +space1.getTheColumn()+","+ space1.getTheFloor()+","+status+"],";
         }
         if(status ==6){
            outCheck = outCheck +"[" +space1.getTheColumn()+","+ space1.getTheFloor()+","+status+"],";
         }
      }

      Map<String, Object> map = new HashMap<>();
      map.put("empty", "["+empty+"]");
      map.put("full", "["+full+"]");
      map.put("inAllot", "["+inAllot+"]");
      map.put("outAllot", "["+outAllot+"]");
      map.put("inCheck", "["+inCheck+"]");
      map.put("outCheck", "["+outCheck +"]");
      map.put("floor", floor);
      map.put("column", column);
      return map;
   }

   @Override
   public String getMessage(BasCargoSpace space) {

      String msg = null;
      List<BasStock> list = basStockMapper.findByThePlatoonAndTheColumnAndTheFloor(space);
      if (list.size()>0 && list!=null){
         msg = "托盘号:" + list.get(0).getTrayCode()+";<br>";
         for (BasStock basStock : list) {
            msg = msg+ " 物料名称:" + basStock.getLineId() + " 比例号:" + basStock.getProductDetailId()+ " 数量:" + basStock.getQuantity()+";<br>";
         }
      }
      Map<String, String> map = new HashMap<>();
      map.put("msg", msg);
      return msg;

   }
   @Override
   public Map<String, long[]> getMydojobFinish(MydoJobFinish mydoJobFinish, int a) {
      List<BasCargoSpace> list = basCargoSpaceMapper.findRoadway(mydoJobFinish.getWarehouseId());
      long[] in = new long[list.size()];
      mydoJobFinish.setInOrOut(String.valueOf(a));
      mydoJobFinish.setStatus("4");
      for(int i=0;i<list.size();i++){
         if(a<=10) {
            mydoJobFinish.setRoadwayId(list.get(i).getRoadwayId());
            long inLong = mydoJobFinishMapper.countInFinish(mydoJobFinish);
            in[i] = inLong;
         }else{
            mydoJobFinish.setRoadwayId(list.get(i).getRoadwayId());
            long inLong = mydoJobFinishMapper.countOutFinish(mydoJobFinish);
            in[i] = inLong;
         }
      }
      Map<String, long[]> map = new HashMap<>();
      map.put("in", in);
      return map;
   }
   @Override
   public Map<String, String[]> getRoadway(String warehouseId) {
      Map<String, String[]> map = new HashMap<>();
      List<BasCargoSpace> list = basCargoSpaceMapper.findRoadway(warehouseId);
      String [] roadway=new String[list.size()];
      for(int i=0;i<list.size();i++){
         roadway[i] = list.get(i).getRoadwayId()+"巷道";
      }
      map.put("roadway", roadway );
      return map;
   }

   @Override
   public Map<String, long[]> getDdjFinish(MydoJobFinish mydoJobFinish) {
      List<BasCargoSpace> list = basCargoSpaceMapper.findRoadway(mydoJobFinish.getWarehouseId());
      long[] in = new long[list.size()];
      long[] out = new long[list.size()];
      WmsDeviceExecutionQueue wms = new WmsDeviceExecutionQueue();
      wms.setScsj(mydoJobFinish.getStartTime());
      wms.setWarehouseId(mydoJobFinish.getWarehouseId());
      for(int i=0;i<list.size();i++){
            wms.setDevId(Long.parseLong(list.get(i).getRoadwayId()));

            long inLong = wdeqfm.countInFinish(wms);
            in[i] = inLong;

            long outLong = wdeqfm.countOutFinish(wms);
            out[i] = outLong;

      }
      Map<String, long[]> map = new HashMap<>();
      map.put("in", in);
      map.put("out", out);
      return map;
   }

   @Override
   public List<Map<String, Object>> getRoadWayData() {
      List<String> list = basCargoSpaceMapper.selectRaadWayId();
      List<Map<String, Object>> maps = new ArrayList<>();
      for (String s : list) {
         Map<String, Object> map = new HashMap<>();
         map.put("v", s);
         map.put("n", s + "巷道");
         List<String> plist = basCargoSpaceMapper.selectThePlatoon(s);
         List<Map<String, Object>> pdata = new ArrayList<>();
         for (String p : plist) {
            Map<String, Object> map1 = new HashMap<>();
            map1.put("v", p);
            map1.put("n", p + "排");
            map1.put("s", null);
            pdata.add(map1);
         }
         map.put("s", pdata);
         maps.add(map);
      }
      return maps;
   }

   /**
    * 巷道均衡分析
    */
   @Override
   public long[] getMydojobFinish(String startTime, String finishTime,int a,String warehouseId) {

      MydoJob mydoJobFinish = new MydoJob();
      mydoJobFinish.setStartTime(startTime);
      mydoJobFinish.setFinishTime(finishTime);
      if (warehouseId.equals("01")){
         long[] in = new long[6];
         if (a == 10){
            mydoJobFinish.setInOrOut("IN");
            for (int i = 0; i < 6; i++){
               mydoJobFinish.setRoadwayId(String.valueOf(i + 1));
               long inLong = mydoJobFinishMapper.countFinish(mydoJobFinish);
               in[i] = inLong;
            }
         } else {
            mydoJobFinish.setInOrOut("OUT");
            for (int i = 0; i < 6; i++){
               mydoJobFinish.setRoadwayId(String.valueOf(i + 1));
               long inLong = mydoJobFinishMapper.countFinish(mydoJobFinish);
               in[i] = inLong;
            }
         }

         return in;
      }else {
         long[] in = new long[3];
         if (a == 10){
            mydoJobFinish.setInOrOut("IN");
            for (int i = 0; i < 3; i++){
               mydoJobFinish.setRoadwayId(String.valueOf(i + 1));
               long inLong = mydoJobFinishMapper.countFinish(mydoJobFinish);
               in[i] = inLong;
            }
         } else {
            mydoJobFinish.setInOrOut("OUT");
            for (int i = 0; i < 3; i++){
               mydoJobFinish.setRoadwayId(String.valueOf(i + 1));
               long inLong = mydoJobFinishMapper.countFinish(mydoJobFinish);
               in[i] = inLong;
            }
         }

         return in;
      }

   }
}
